package com.itheima.service.permission.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Permission;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.permission.PermissionMapper;
import com.itheima.service.permission.PermissionService;
import com.itheima.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.UUID;

public class PermissionServiceImpl implements PermissionService {
    /**实现接口,查询所有权限信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<Permission> findAll(String pageNum, String pageSize) {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSession对象
            session = MapperFactory.getSqlSession();
            //通过session对象获取mapper的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //设置默认当前页为1
            int pageN=1;
            //设置每页条目数为10
            int pageS=10;
            //若页数不为空  转换数据类型
            if(StringUtils.isNoneBlank(pageNum)){
                pageN=Integer.parseInt(pageNum);
            }
            //若条目数不为空  转换数据类型
            if(StringUtils.isNoneBlank(pageSize)){
                pageS=Integer.parseInt(pageSize);
            }
            //设置分页参数:当前页面,每页显示的条目数
            Page<Permission> page= PageHelper.startPage(pageN,pageS);
            //调用方法
            mapper.findAll();
            return page;
        }catch(Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //释放资源
            TransactionUtil.close(session);
            //关闭资源
            TransactionUtil.close(session);
        }
    }

    /**
     * 实现接口,查询所有权限信息
     * @return
     */
    @Override
    public List<Permission> findAll() {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSession对象
            session = MapperFactory.getSqlSession();
            //通过session对象获取mapper的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //调用方法
            List<Permission> list = mapper.findAll();
            return list;
        }catch(Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //释放资源
            TransactionUtil.close(session);
            //关闭资源
            TransactionUtil.close(session);
        }
    }


    /**
     * 实现新增信息接口
     * @param permission
     */
    @Override
    public void save(Permission permission) {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSeesion对象
            session = MapperFactory.getSqlSession();
            //获取代理对象的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //处理主键id
            String id = UUID.randomUUID().toString().replace("-", "");
            //向permission添加id
            permission.setId(id);
            //实现接口方法
            mapper.save(permission);
        }catch (Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //提交事务
            TransactionUtil.commit(session);
            //释放资源
            TransactionUtil.close(session);
        }

    }

    /**
     * 批量删除权限信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteByIds(String ids) {
        //SqlSession初始化
        SqlSession session= null;
        boolean flag = true;
        try {
            //获取SqlSeesion对象
            session = MapperFactory.getSqlSession();
            //获取代理对象的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //获得id数组
            String[] id = ids.split(",");
            //判断是否删除成功
            if (flag) {
                mapper.deleteByIds(id);
            } else {
                flag = false;
            }
        }catch (Exception e) {
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //提交事务
            TransactionUtil.commit(session);
            //释放资源
            TransactionUtil.close(session);
        }
        return flag;
    }

    /**
     * 根据id删除权限数据
     * @param id
     */
    @Override
    public void deleteById(String id) {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSeesion对象
            session = MapperFactory.getSqlSession();
            //获取代理对象的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //实现mapper接口
            mapper.deleteById(id);
        }catch (Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //提交事务
            TransactionUtil.commit(session);
            //释放资源
            TransactionUtil.close(session);
        }
    }

    @Override
    public void deleteByRidAndPid(String id) {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSeesion对象
            session = MapperFactory.getSqlSession();
            //获取代理对象的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //实现mapper接口
            mapper.deleteByRidAndPid(id);
        }catch (Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //提交事务
            TransactionUtil.commit(session);
            //释放资源
            TransactionUtil.close(session);
        }
    }

    /**
     * 批量删除role_permission表信息
     * @param ids
     */
    @Override
    public void deleteByRidAndPids(String ids) {
        //SqlSession初始化
        SqlSession session= null;
        boolean flag = true;
        try {
            //获取SqlSeesion对象
            session = MapperFactory.getSqlSession();
            //获取代理对象的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //获得id数组
            String[] id = ids.split(",");
            //判断是否删除成功
            if (flag) {
                mapper.deleteByRidAndPids(id);
            } else {
                deleteByIds(ids);
            }
        }catch (Exception e) {
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //提交事务
            TransactionUtil.commit(session);
            //释放资源
            TransactionUtil.close(session);
        }
    }

    @Override
    public Page<Permission> findByName(String pageNum,String pageSize,String name) {
        //SqlSession初始化
        SqlSession session= null;
        try {
            //获取SqlSession对象
            session = MapperFactory.getSqlSession();
            //通过session对象获取mapper的值
            PermissionMapper mapper = session.getMapper(PermissionMapper.class);
            //设置默认当前页为1
            int pageN=1;
            //设置每页条目数为10
            int pageS=10;
            //若页数不为空  转换数据类型
            if(StringUtils.isNoneBlank(pageNum)){
                pageN=Integer.parseInt(pageNum);
            }
            //若条目数不为空  转换数据类型
            if(StringUtils.isNoneBlank(pageSize)){
                pageS=Integer.parseInt(pageSize);
            }
            //设置分页参数:当前页面,每页显示的条目数
            Page<Permission> page= PageHelper.startPage(pageN,pageS);
            //调用方法
            List<Permission> list=mapper.findByName(name);
            return page;
        }catch(Exception e){
            //事务回滚
            TransactionUtil.rollback(session);
            throw new RuntimeException(e);
        }finally {
            //释放资源
            TransactionUtil.close(session);
            //关闭资源
            TransactionUtil.close(session);
        }
    }
}
